package com.example.easybuy.controller;

import com.example.easybuy.pojo.EasybuyProduct;
import com.example.easybuy.pojo.EasybuyProductCategory;
import com.example.easybuy.pojo.Product;
import com.example.easybuy.service.EasybuyProductCategoryService;
import com.example.easybuy.service.EasybuyProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/easybuyProduct")
public class EasybuyProductController {

    @Autowired
    private EasybuyProductCategoryService easybuyProductCategoryService;

    @Autowired
    private EasybuyProductService easybuyProductService;

    //根据商品id获取商品（商品详情页）
    @RequestMapping("getProductInfo")
    public Object getProductInfo(Integer id) {
        //通过ID获取商品
        EasybuyProduct product = easybuyProductService.getProductById(id);
        int lev[] = {product.getCategoryLevel1Id(), product.getCategoryLevel2Id(), product.getCategoryLevel3Id()};
        List<String> nameList = new ArrayList<String>();
        for (int levId : lev) {
            EasybuyProductCategory productCategory = easybuyProductCategoryService.getProductByLevelId(levId);
            if (productCategory != null) {
                nameList.add(productCategory.getName());
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("easybuyproduct", product);
        map.put("nameList", nameList);
        //获取产品分类
        List<EasybuyProductCategory> categoryList = easybuyProductCategoryService.allProductCategory();
        map.put("categoryList", categoryList);
        return map;
    }

    //传入查询条件，通过solr获取商品列表
    @RequestMapping("/getProductListPage")
    public Object getProductList(String condition, String name,
                                 String categoryLevel1Id, String categoryLevel2Id,
                                 String categoryLevel3Id, Integer pageNum, String price) {
        Map<String, Object> someMap = new HashMap<>();
       /* String condition="default";
        String name ="椰子粉";
        String categoryLevel1Id="";
        String categoryLevel2Id="661";
        String categoryLevel3Id="";
        Integer pageNum=1;
        String price="*-*";*/
        someMap.put("condition", condition);
        someMap.put("name", name);
        someMap.put("categoryLevel1Id", categoryLevel1Id);
        someMap.put("categoryLevel2Id", categoryLevel2Id);
        someMap.put("categoryLevel3Id", categoryLevel3Id);
        someMap.put("pageNum", pageNum);
        someMap.put("price", price);
        Map<String, Object> map = easybuyProductService.getProductFromSolr(someMap);
        System.out.println("-------------" + categoryLevel3Id);
        return map;
    }
}
